package park.log;

import org.apache.log4j.RollingFileAppender;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExtendedRollingFileAppender extends RollingFileAppender
{
    @Override
    public synchronized void setFile(
            String fileName, boolean append, boolean bufferedIO,
            int bufferSize) throws IOException
    {
        File file = new File(fileName);
        if (!file.exists())
        {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        else
        {
            file.renameTo(new File(fileName + "." + (new SimpleDateFormat("YYYYMMdd_HHmmss_SSSS").format(new Date()))));
            file.createNewFile();
        }
        super.setFile(fileName, append, bufferedIO, bufferSize);
    }
}
